package com.edu.zut.hb.software.server.controller;

import com.edu.zut.hb.software.server.pojo.registerVo.DefaultRegisterVo;
import com.edu.zut.hb.software.server.pojo.registerVo.StudentRegisterVo;
import com.edu.zut.hb.software.server.service.RegisterService;
import com.edu.zut.hb.software.server.pojo.AdminLoginParam;
import com.edu.zut.hb.software.server.pojo.RespBean;
import com.edu.zut.hb.software.server.service.IAdminService;
import com.edu.zut.hb.software.server.service.IStudentService;
import com.edu.zut.hb.software.server.service.ITeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Objects;

/**
 * 登录控制器
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Api(tags = "AccountController")
@RestController
public class AccountController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private RegisterService registerService;


    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request) {
//        System.out.println("username:" + adminLoginParam.getUsername());
//        System.out.println("password:" + adminLoginParam.getPassword());
//        System.out.println("username长度:" + (adminLoginParam.getUsername()).length());
        if ((adminLoginParam.getUsername()).length() == 12) {
            //学生
            return studentService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), adminLoginParam.getCode(), request);
        } else if ((adminLoginParam.getUsername()).length() == 7) {
            //老师
            return teacherService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), adminLoginParam.getCode(), request);
        } else {
            //管理员
            return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), adminLoginParam.getCode(), request);
        }
        // return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), adminLoginParam.getCode(), request);
    }

    @ApiOperation(value = "注册并存入数据库")
    @PostMapping("/register")
    public RespBean register(@RequestBody StudentRegisterVo registerVo, HttpServletRequest request) {
        return registerService.registerStudent(registerVo);
    }


    @ApiOperation(value = "获取登录信息")
    @GetMapping("/userinfo")
    public Serializable getAdminInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (Objects.nonNull(authentication)) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) principal;
                String id = userDetails.getUsername();
                if (id.length() == 12) {//学生
                    return studentService.getById(id);
                }
                if (id.length() == 7) {//教师
                    return teacherService.getById(id);
                } else return adminService.getAdminByUserName(id);
            } else return null;
        } else return null;
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout() {
        return RespBean.success("注销成功！");
    }
}